<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <!--<el-input v-model="query.value" clearable placeholder="可输入用户ID、用户名称" style="width: 200px;" class="filter-item" @keyup.enter.native="initMethod"/>-->
      <span>申请类型：</span>
      <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="initMethod">搜索</el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="tableData.content" stripe border size="small" style="width: 100%;">
      <el-table-column prop="id" label="编号"/>
      <el-table-column prop="openid" label="用户openid" show-overflow-tooltip/>
      <el-table-column prop="identity.avatarUrl" label="头像" align="center">
        <template slot-scope="scope">
          <el-avatar v-if="scope.row.identity" :src="scope.row.identity.avatarUrl" :size="50" fit="fill" shape="square"/>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名称" show-overflow-tooltip/>
      <el-table-column prop="type" label="用户类型"/>
      <el-table-column prop="weixin" label="微信号"/>
      <el-table-column prop="tel" label="联系电话"/>
      <el-table-column prop="identity.nickName" label="微信昵称"/>
      <el-table-column prop="channel" label="铺货渠道"/>
      <el-table-column prop="province" label="铺货省份"/>
      <el-table-column prop="city" label="铺货城市"/>
      <el-table-column prop="area" label="铺货地区"/>
      <el-table-column prop="address" label="详细地址" width="100px" show-overflow-tooltip/>
      <el-table-column prop="created" label="申请时间" show-overflow-tooltip/>
      <el-table-column label="操作" fixed="right" width="180px" align="center">
        <template slot-scope="scope">
          <el-popover v-permission="['admin','identity:del']" :ref="'approval-'+scope.row.id" placement="top" width="180">
            <p>确定同意申请吗？</p>
            <el-form ref="approbalForm" :model="approbalForm" label-width="70px" label-position="left">
              <el-form-item label="名称" prop="approvalvalue">
                <el-input v-model="approbalForm.approvalvalue"/>
              </el-form-item>
              <el-form-item :rules="[{ required: true, message: '比例不能为空'},{ type: 'number', message: '比例必须为数字值'}]" label="提成比例" prop="approvalSize">
                <el-input v-model.number="approbalForm.approvalSize"/>
              </el-form-item>
              <el-form-item style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs['approval-'+scope.row.id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="approval(scope.row.id)">确定</el-button>
              </el-form-item>
            </el-form>
            <el-button slot="reference" type="success" size="mini">同意</el-button>
          </el-popover>

          <el-popover :ref="'cancel-'+scope.row.id" placement="top" width="180">
            <p>确定拒绝申请吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs['cancel-'+scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="cancel(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini">拒绝</el-button>
          </el-popover>

        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <div class="block">
      <el-pagination
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.size"
        :total="tableData.totalElements"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { postRequest } from '@/api/identity'
export default {
  data() {
    return {
      loading: false,
      delLoading: false,
      query: {
        value: '',
        type: 'agent',
        page: 1,
        size: 10
      },
      approbalForm: {
        approvalSize: 90,
        approvalvalue: ''
      },
      tableData: {
        totalElements: 0,
        content: []
      },
      queryTypeOptions: [
        { key: 'agent', display_name: '代理' },
        { key: 'shop', display_name: '商户' }
      ]
    }
  },
  created() {
    if (Object.keys(this.$route.query).length !== 0) {
      this.query.value = this.$route.query.value
    }
    this.$nextTick(() => {
      this.initMethod()
    })
  },
  methods: {
    checkPermission,
    initMethod() {
      console.log(this.query)
      postRequest('/pc/apply', this.query).then(resp => {
        console.log(resp)
        this.tableData = resp.entity
      })
    },
    approval(id) {
      const data = {
        page: id,
        size: this.approbalForm.approvalSize,
        value: this.approbalForm.approvalvalue,
        type: 'approval'
      }
      this.$refs['approbalForm'].validate((valid) => {
        if (valid) {
          postRequest('/pc/processApply', data).then(resp => {
            this.initMethod()
            this.$refs[`approval-` + id].doClose()
            this.approbalForm.approvalSize = 90
            this.approbalForm.approvalvalue = ''
            if (resp.code === 500) {
              this.$notify({
                title: '警告',
                message: resp.message,
                type: 'warning'
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel(id) {
      const data = {
        page: id,
        size: 90,
        value: 'name',
        type: 'refuse'
      }
      postRequest('/pc/processApply', data).then(resp => {
        this.initMethod()
        this.$refs[`cancel-` + id].doClose()
      })
    },
    handleSizeChange(val) {
      this.query.size = val
      this.initMethod()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.initMethod()
    }
  }
}
</script>

<style scoped>
  .el-button + .el-button {
    margin-left: 0;
  }
</style>
